package com.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductMapper mapper;

    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum, String info, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name != null && name.length()!=0 ){
            qw.like("p_name",name);
        }
        if (startNum != null){
            qw.ge("p_num",startNum);
        }
        if (endNum !=null){
            qw.le("p_num",endNum);
        }
        if (info != null && info.length()!=0){
            if (info.equals("不限")){
                qw.isNotNull("p_sellPrice");
            }
            if (info.equals("未定销售价")){
                qw.isNull("p_sellPrice");
            }
        }
        if (type !=null && type.length()!=0){
            qw.like("p_type",type);
        }
        return mapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public ProductBean findById(String productId) {
        return mapper.selectById(productId);
    }

    @Override
    public void updatePrice(String product, Integer price) {
        ProductBean productBean = mapper.selectById(product);
        productBean.setSellPrice(price);
        mapper.updateById(productBean);
    }
}
